﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HousingInfo.Library;
using System.IO;

namespace WebHousingInfo
{
    public partial class Quanlyvanban : System.Web.UI.Page
    {
        private static string mode = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["UserName"] == null)
                    Response.Redirect("DangNhap.aspx");
                mode = Request.QueryString["xem"];
                BindGrid();
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Xem")
            {
                GridViewRow selectrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                Response.Redirect("ChiTietVanBan.aspx?masach=" + selectrow.Cells[0].Text);
            }
            else if (e.CommandName == "download")
            {
                GridViewRow selectrowdow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                string Title = ((Label)(((Control)e.CommandSource).Parent.Parent).FindControl("url")).Text;
                string pathFileImage = Server.MapPath("TotalUpload/VanBan/" + Title);
                if (File.Exists(pathFileImage))
                {
                    Response.Redirect("TotalUpload/VanBan/" + Title);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Văn bản này không tồn tại.')", true);
                }
            }
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            this.BindGrid();
        }

        public void BindGrid()
        {
            if (mode == "dangdang")
            {
                GridView1.DataSource = TextHouseBOList.GetTextHouse_ByUser(Session["UserName"].ToString(), "dangdang");
                GridView1.DataBind();
            }
            else if (mode == "choduyet")
            {
                GridView1.DataSource = TextHouseBOList.GetTextHouse_ByUser(Session["UserName"].ToString(), "choduyet");
                GridView1.DataBind();
            }
            else
            {
                GridView1.DataSource = TextHouseBOList.GetTextHouse_ByUser(Session["UserName"].ToString(), "tatca");
                GridView1.DataBind();
            }
        }
    }
}
